/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.evento.model.entity;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name = "evento", catalog = "eventos", schema = "public")
@NamedQueries({
    @NamedQuery(name = "Evento.findAll", query = "SELECT e FROM Evento e"),
    @NamedQuery(name = "Evento.findById", query = "SELECT e FROM Evento e WHERE e.id = :id"),
    @NamedQuery(name = "Evento.findByDataAndIdUsuario", query = "SELECT e FROM Evento e WHERE e.data = :data AND e.idUsuarioGAE = :idUsuario"),
    @NamedQuery(name = "Evento.findBydIdUsuario", query = "SELECT e FROM Evento e WHERE e.idUsuarioGAE = :idUsuario")})
public class Evento implements EntityBase, Comparable<Evento> {
	
	private static final long serialVersionUID = 1L;
	public static final String NAMED_QUERY_FIND_BY_DATA_AND_ID_USUARIO = "findByDataAndIdUsuario";
	public static final String NAMED_QUERY_FIND_BY_ID_USUARIO = "findBydIdUsuario";
	
	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Long id;
    @Basic(optional = false)
    @Column(name = "data", nullable = false)
    @Temporal(TemporalType.DATE)
    private Date data;
    @Basic(optional = false)
    @Column(name = "hora", nullable = false, length = 5)
    private String hora;
    @Basic(optional = false)
    @Column(name = "titulo", nullable = false, length = 100)
    private String titulo;
    @Column(name = "descricao", length = 255)
    private String descricao;
    @Basic(optional = false)
    @Column(name = "concluido", nullable = false)
    private boolean concluido;
    @Transient
    private Usuario usuario;
    @Column(name = "id_usuario_gae", nullable = false)
    private Long idUsuarioGAE ;

    public Evento() {
    }

    public Evento(Long id) {
        this.id = id;
    }

    public Evento(Long id, Date data, String hora, String titulo, boolean concluido) {
        this.id = id;
        this.data = data;
        this.hora = hora;
        this.titulo = titulo;
        this.concluido = concluido;
    }

    public Evento(Usuario usuario, Date data) {
    	this.usuario = usuario;
    	this.data = data;
	}

	@Override
	public Long getId() {
        return id;
    }

    @Override
	public void setId(Long id) {
        this.id = id;
    }

    public Date getData() {
        return data;
    }

    public void setData(Date data) {
        this.data = data;
    }
    
    public Date getDataHora() {
    	try {
			DateFormat dfAntes = new SimpleDateFormat("dd/MM/yyyy");
			DateFormat dfDepois = new SimpleDateFormat("dd/MM/yyyy HH:mm");
			return dfDepois.parse(dfAntes.format(data) + " " + hora);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
    }
    
    public void setDataHora(Date data) {
    	// TODO
    }

    public String getHora() {
        return hora;
    }

    public void setHora(String hora) {
        this.hora = hora;
    }

    public String getTitulo() {
        return titulo;
    }

    public void setTitulo(String titulo) {
        this.titulo = titulo;
    }

    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    public boolean getConcluido() {
        return concluido;
    }

    public void setConcluido(boolean concluido) {
        this.concluido = concluido;
    }

    public Usuario getUsuario() {
        return usuario;
    }

    public void setUsuario(Usuario idUsuario) {
        this.usuario = idUsuario;
    }

    public Long getIdUsuarioGAE() {
		return idUsuarioGAE;
	}

	public void setIdUsuarioGAE(Long idUsuarioGAE) {
		this.idUsuarioGAE = idUsuarioGAE;
	}

	@Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Evento)) {
            return false;
        }
        Evento other = (Evento) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

	@Override
	public int compareTo(Evento o) {
		if (this.data.compareTo(o.getData()) == 0) {
			if (this.hora.compareToIgnoreCase(o.getHora()) == 0) {
				return this.titulo.compareTo(o.getTitulo());
			} else {
				return this.hora.compareToIgnoreCase(o.getHora());
			}
		} else {
			return this.data.compareTo(o.getData());
		}
	}
    
}